<template>
  <div id="app" style="width:50%;margin:30px auto;">
    <h1 align="center">注册界面</h1>
    <el-form ref="form" :model="usermodel" label-width="80px">
      <el-form-item label="姓名">
        <el-input placeholder="请输入姓名" v-model="usermodel.name"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input placeholder="请输入昵称" v-model="usermodel.loginName"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input placeholder="请输入密码" v-model="usermodel.password" show-password></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="usermodel.gender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生日">
        <el-date-picker
            v-model="usermodel.birthday"
            type="date"
            format="yyyy年MM月dd日"
            placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="电话">
        <el-input placeholder="请输入电话" v-model="usermodel.phonenumber"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input placeholder="请输入地址" v-model="usermodel.address"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="register">注册</el-button>
        <el-button type="info" @click="cancel">取消</el-button>
        <el-button type="login" @click="login">立即登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "RegisterView",
  data(){
    return{
      usermodel:{
        name:'',
        loginName:'',
        password:'',
        age:'',
        gender:'',
        birthday:'',
        phonenumber:'',
        address:''
      }
    }
  },
  methods:{

    register(){
      //alert('注册')
      //以二进制数据格式，传递表单数据
      let data = new FormData();
      //迭代usermodel对象中的数据，封装至FormData中
      for(let key in this.usermodel){
        data.append(key , this.usermodel[key])
      }
      this.usermodel.birthday = new Date(this.usermodel.birthday).Format('yyyy-MM-dd')
      data.set('birthday' , this.usermodel.birthday)
      axios.post('register' , data)
          .then(response=>{
            //console.log(response)
            let code = response.data.code
            let msg = response.data.msg
            if(code==200){
              this.$message({showClose: true,message: msg,type: 'success'});
              //跳转至用户列表页面
            }else{
              this.$message({showClose: true,message: msg,type: 'error'});
            }
          })
          .catch(error=>{
            console.log(error)
          })
    },
    cancel(){
      this.usermodel={name:'',loginName: '',password: '',age:'',gender:'',
        birthday:'', phonenumber:'', address:''}
    },

    login(){
      this.$router.replace('/')
    }
  }
}
</script>

<style scoped>

</style>